<?php

namespace App\Http\Controllers\Admin;

use App\Model\Category;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $cate = new Category();
        $cates = $cate->gettree();
        return view('admin.category.list',compact('cates'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //获取上级id
        if($request->has('cate_id')){
            $cate_id =$request->get('cate_id');
        }else{
            $cate_id = false;
        }
        //获取栏目
        $cate = Category::all();
        return view('admin.category.cate_add',compact('cate','cate_id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $input = $request->except('_token');
        //       1.数据合法性验证
        $rule = [
            'cate_name' => 'required',
            'cate_title' => 'required',
        ];
        $messages = [
            'cate_name.required' => '分类名称不能为空!',
            'cate_title.required' => '分类别名不能为空!'
        ];
        $validator = Validator::make($request->all(),$rule,$messages);
        if($validator->fails()){
            $errors = $validator->errors()->toArray();
            $res = [
                'status'=>1,
                'data'=>$errors,
                'message'=>'验证失败'
            ];
            return json_encode($res);
        }
        //保存数据到数据库中
        $cate = new Category();
        $cate->cate_pid = 0;
        if($input['cate_pid']){
            $cate->cate_pid = $input['cate_pid'];
        }
        $cate->cate_name = $input['cate_name'];
        $cate->cate_title = $input['cate_title'];
        $cate->cate_order = 0;
        $status = $cate->save();
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'添加成功'
            ];
        }else{
            $res = [
                'status'=>1,
                'data'=>['添加失败，请检查数据库'],
                'message'=>'添加失败'
            ];
        }
        return json_encode($res);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $cateinfo = Category::find($id);
        //获取栏目
        $cate = Category::all();
        return view('admin.category.cate_edit',compact('cate','cateinfo'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $input = $request->except('_token');
        $rule = [
            'cate_name' => 'required',
            'cate_title' => 'required',
        ];
        $messages = [
            'cate_name.required' => '分类名称不能为空!',
            'cate_title.required' => '分类别名不能为空!'
        ];
        $validator = Validator::make($request->all(),$rule,$messages);
        if($validator->fails()){
            $errors = $validator->errors()->toArray();
            $res = [
                'status'=>1,
                'data'=>$errors,
                'message'=>'验证失败'
            ];

        }
        $cate = new Category();
        $status = $cate->where('cate_id',$input['cate_id'])->update($input);
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'修改成功'
            ];

        }else{
            $res = [
                'status'=>1,
                'data'=>['修改失败'],
                'message'=>'修改失败'
            ];
        }
        return json_encode($res);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
    public function checkOrder(Request $request){

        $cate = Category::find($request->cate_id);
        $cate->cate_order = $request->cate_order;
        $status = $cate->save();
        if($status){
            $res = [
                'status'=>0,
                'data'=>[],
                'message'=>'排序成功'
            ];
        }else{
            $res = [
                'status'=>1,
                'data'=>['排序失败，请检查数据库'],
                'message'=>'排序失败'
            ];
        }
        return json_encode($res);
    }

}
